<?php
namespace Admin\Controller;

class IndexController extends BaseController{
	private $overview;
	 
	public function _initialize(){
		parent::_initialize();
		$this->overview = ROOT_PATH.'/App/Admin/Conf/overview.php';
	}
	
	public function index(){
		$this->assign('Version',explode('|',C('VERSION')));
    	$this->display();
    }
    
    public function main(){
		if (!file_exists($this->overview) || time()-filemtime($this->overview)>18000){
			$this->update(0);
			$this->success(U(CONTROLLER_NAME.'/main'));
		}else{
			$this->assign('LastUpdate',date('Y-m-d H:i:s',filemtime($this->overview)));
			$this->assign('NextUpdate',date('Y-m-d H:i:s',filemtime($this->overview)+18000));
		}
		$this->assign('Version',explode('|',C('VERSION')));
    	$this->display();
    }
    
    public function update($type=1){
    	$Order = D('Order');
    	$Product = D('Product');
    	$output = '<?php';
    	$output .= "\r\nreturn array(";
    	$output .= "\r\n\t'ORDER_NUM1'=>'".$Order->total()."',";
    	$output .= "\r\n\t'ORDER_NUM2'=>'".$Order->total2()."',";
    	$output .= "\r\n\t'ORDER_NUM3'=>'".$Order->total3()."',";
    	$output .= "\r\n\t'ORDER_NUM4'=>'".$Order->total4()."',";
    	$output .= "\r\n\t'PRODUCT_NUM1'=>'".$Product->total()."',";
    	$output .= "\r\n\t'PRODUCT_NUM2'=>'".$Product->total2()."'";
    	$output .= "\r\n);\r\n?>";
    	$type ? file_put_contents($this->overview,$output) ? $this->success(U(CONTROLLER_NAME.'/main'),'统计更新成功！') : $this->failed('统计更新失败，请检查App/Admin/Conf目录权限！') : file_put_contents($this->overview,$output);
    }
}